package com.documents.common.util;

import com.documents.common.Config;
import java.util.Properties;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2008</p>
 *
 * <p>Company: </p>
 *
 * @author not attributable
 * @version 1.0
 */
public class SystemConfig
{

    private String filename;
    private Properties p;
    private FileInputStream in;
    private FileOutputStream out;
    private static String SYSTEM_MSM_PROPERTIES_PATH =  Config.getClassesPath() +"systemparams.properties";;
  /**



   * @param filename string

   */


  private static SystemConfig instance  ;
  public static SystemConfig getInstance()
  {
      if(instance==null)
      {
          instance = new SystemConfig(SYSTEM_MSM_PROPERTIES_PATH);
      }
      return instance ;
  }
  private SystemConfig(String filename)
  {

      this.filename = filename;

      File file = new File(filename);

      try {

          in = new FileInputStream(file);

          p = new Properties();



          p.load(in);


          in.close();


      }
      catch (FileNotFoundException e)
      {

          e.printStackTrace();

      }
      catch(java.io.IOException ie)
      {
          ie.printStackTrace();
      }
  }

  /**



   * @param itemname string

   * @return string

   */

  public String getvalue(String itemname){

    return p.getProperty(itemname);

  }


   /**


   * @param itemname string

   * @param value string

   */

  public void setvalue(String itemname,String value){

    p.setProperty(itemname,value);
    return;
  }

/**



   * @param filename string

   * @param description string

   * @throws exception

   */
  public void savefile(String filename,String description)throws Exception
  {

      try {

          File f = new File(filename);

          out = new FileOutputStream(f);

          p.store(out, description);

          out.close();

      }

      catch (IOException ex) {

          throw new Exception("" + filename);

      }
  }

      /**
       *

       * @param filename string

       * @throws exception

       */

      public void savefile(String filename)  throws Exception {

          savefile(filename, "");

      }



  /**


   * @throws exception

   */

  public void savefile() throws Exception {

    if(filename.length()==0)

      throw new Exception("error");

    savefile(filename);

  }

  /**



   * @param value string

   */

  public void deletevalue(String value){

    p.remove(value);

  }

  /**



  */

  public void list() {
      p.list(System.out);
  }


  /**

   * main method for test

   * @param args string[]

   */

  public static void main(String[] args) {

    String file =SYSTEM_MSM_PROPERTIES_PATH;

    SystemConfig pu = new SystemConfig(file);

    pu.list();
//    String ip = pu.getvalue("socketip");
//    String port = pu.getvalue("socketport");
//    System.out.println("ip="+ip+";  port="+port);
    

  }





}
